﻿using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Module.StatisticalReport
{
    public partial class WeekCapitalStatistics : BaseUI.BasePage
    {
        private QX_PLANYEARAPPLYBLL planYearApplyBLL = new QX_PLANYEARAPPLYBLL();
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                QUERY_CLICK(sender, e);
            }
        }

        protected void QUERY_CLICK(object sender, EventArgs e)
        {
            var where = "";
            var cmyid = FORM_COMPANY.Value;
            if (cmyid == -1)
            {
                cmyid = CurrentUserCompanyID;
            }

            var cmyIds = companyBLL.GetCompanyFatherAndChildByID(cmyid);
            if (cmyIds == "")
            {
                MessageBox.Show(Page, "当前登录人公司已经不存在！");
                return;
            }
            where += " and t.N_COMPANY_ID IN(" + cmyIds + ")";

            if (FORM_BEGIN_DATE.Value != "")
            {
                where += " AND t.D_START >= TO_DATE('" + Convert.ToDateTime(FORM_BEGIN_DATE.Value).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS')";
            }
            if (FORM_OVER_DATE.Value != "")
            {
                where += " AND t.D_START <= TO_DATE('" + Convert.ToDateTime(FORM_OVER_DATE.Value).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS')";
            }
            ViewState["WHERE"] = where;
            BindData(true);
        }

        protected void SORTING_EVENT(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["ORDER"] == null ? null : (string)ViewState["ORDER"];
            OrderDire = ViewState["ORDER_TYPE"] == null ? null : (string)ViewState["ORDER_TYPE"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "DESC")
                    OrderDire = "ASC";
                else
                    OrderDire = "DESC";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["ORDER"] = SortOrder;
            ViewState["ORDER_TYPE"] = OrderDire;
            QUERY_CLICK(sender, e);
        }

        protected void BindData(bool iffirst)
        {
            var whereStr = ViewState["WHERE"] as string;
            var orderStr = ViewState["ORDER"] as string;
            var orderTypeStr = ViewState["ORDER_TYPE"] as string;
            var sql = @"select c.cmy_name,
       t.n_year,
       t.n_month,
       t.n_week,
       t.c_week_name,
       t.c_useweek,
       t.d_operator_time,
       t.d_start,
       t.d_end,
       s.user_name, t.n_business_id,
       t.n_weekapp_id
  from QS_PLANWEEKAPPLY t, sys_company c, SYS_USER s
 where t.n_business_id <> 0
   and t.n_business_state = 2
   and t.n_company_id = c.cmy_id
   and t.n_user_id = s.user_id ";
            if (whereStr != null)
            {
                sql += whereStr;
            }
            if (orderStr != null)
            {
                sql += " ORDER BY " + orderStr + " " + orderTypeStr;
            }
            if (iffirst)
            {
                PageIng.CurrentPage = 1;
                PageIng.RowsCount = planYearApplyBLL.SqlCount(sql);
            }
            var dt = planYearApplyBLL.PageQuery(sql, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = TABLE;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.Visible = false;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = TABLE;
                TABLE.DataSource = null;
                TABLE.DataBind();
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            var dt = ((DataTable)ViewState["database"]).Copy();
            dt.Columns["CMY_NAME"].ColumnName = "公司名称";
            dt.Columns["c_week_name"].ColumnName = "业务名称";
            dt.Columns["c_useweek"].ColumnName = "时间";
            dt.Columns["user_name"].ColumnName = "提交人";
            dt.Columns.Remove("N_YEAR");
            dt.Columns.Remove("N_MONTH");
            dt.Columns.Remove("N_WEEK");
            dt.Columns.Remove("D_OPERATOR_TIME");
            dt.Columns.Remove("D_START");
            dt.Columns.Remove("D_END");
            dt.Columns.Remove("N_BUSINESS_ID");
            dt.Columns.Remove("N_WEEKAPP_ID");
            toExecl1(dt, "周资金统计" + DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Millisecond.ToString());
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var key = Guid.NewGuid().ToString();
                Session[key + "Header"] = "周资金统计";

                var dt = ((DataTable)ViewState["database"]).Copy();
                //dt.Columns.Remove("ID");
                dt.Columns.Remove("N_YEAR");
                dt.Columns.Remove("N_MONTH");
                dt.Columns.Remove("N_WEEK");
                dt.Columns.Remove("D_OPERATOR_TIME");
                dt.Columns.Remove("D_START");
                dt.Columns.Remove("D_END");
                dt.Columns.Remove("N_BUSINESS_ID");
                dt.Columns.Remove("N_WEEKAPP_ID");
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToUpper() == "CMY_NAME")
                    {
                        dt.Columns[i].ColumnName = "公司名称";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToUpper() == "C_WEEK_NAME")
                    {
                        dt.Columns[i].ColumnName = "业务名称";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToUpper() == "C_USEWEEK")
                    {
                        dt.Columns[i].ColumnName = "时间";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToUpper() == "USER_NAME")
                    {
                        dt.Columns[i].ColumnName = "提交人";
                    }
                }
                Session[key + "Body"] = dt;
                MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=BASIC&VALUE=" + key + "\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
            }
            catch (Exception)
            {
                MessageBox.Show(Page, "请先点击查询！！");
            }
        }
    }
}